package code.rocky.chapter05;

import java.util.*;

public class Suggest67 {
    public static void main(String[] args) {
        int stuName = 80 * 100000;
        List<Integer> scores = new ArrayList<>(stuName);
        for (int i = 0; i < stuName; i++) {
            scores.add(new Random().nextInt(150));
        }
        long start = System.currentTimeMillis();
        System.out.println("平均分是: " + average(scores));
        System.out.println("执行时间是: " + (System.currentTimeMillis() - start) + " ms");
        long start1 = System.currentTimeMillis();
        System.out.println("平均分是: " + average1(scores));
        System.out.println("执行时间是: " + (System.currentTimeMillis() - start1) + " ms");

        List<Integer> scores1 = new LinkedList<Integer>();
        for (int i = 0; i < stuName; i++) {
            scores1.add(new Random().nextInt(150));
        }
        long start2 = System.currentTimeMillis();
        System.out.println("平均分是: " + average2(scores1));
        System.out.println("执行时间是: " + (System.currentTimeMillis() - start2) + " ms");

        long start3 = System.currentTimeMillis();
        System.out.println("平均分是: " + average3(scores1));
        System.out.println("执行时间是: " + (System.currentTimeMillis() - start3) + " ms");
    }

    private static int average(List<Integer> scores) {
        int sum = 0;
        for (Integer score : scores) {
            sum += score;
        }
        return sum / scores.size();
    }

    private static int average1(List<Integer> scores) {
        int sum = 0;
        for (int i = 0; i < scores.size(); i++) {
            sum += scores.get(i);
        }
        return sum / scores.size();
    }

    private static int average2(List<Integer> scores) {
        int sum = 0;
        for (Integer score : scores) {
            sum += score;
        }
        return sum / scores.size();
    }

    private static int average3(List<Integer> scores) {
        int sum = 0;
        if (scores instanceof RandomAccess) {
            for (int i = 0; i < scores.size(); i++) {
                sum += scores.get(i);
            }
        }
        else {
            for (int score : scores) {
                sum += score;
            }
        }
        return sum / scores.size();
    }
}
